<template>
  <van-radio-group v-model="sex">
    <van-cell-group>
      <van-cell title="男" clickable @click="radio = 0">
        <van-radio name=0></van-radio>
      </van-cell>
      <van-cell title="女" clickable @click="radio = 1">
        <van-radio name=1></van-radio>
      </van-cell>
    </van-cell-group>
  </van-radio-group>
</template>

<script>
  import {RadioGroup, Radio, Cell, CellGroup, Toast} from 'vant';

  export default {
    name: "Sex",
    components: {
      [RadioGroup.name]: RadioGroup,
      [Radio.name]: Radio,
      [Cell.name]: Cell,
      [CellGroup.name]: CellGroup,
      [Toast.name]: Toast,
    },
    data() {
      return {
        sex: "",
      }
    },
    props:["icon"],
    methods: {
      setMsg() {
        let _this = this;
        if (_this.sex !== '0' && _this.sex !== '1') {
          Toast.fail('请选择性别！');
          _this.returnFlag(false);
          return;
        }
        let data = _this.$qs.stringify({
          'sex': _this.sex
        });
        let token = _this.getCookie("token");
        _this.$http.post(_this.apiUrl("user/users/setInfo"), data, {headers: {'token': token}}).then((res) => {
          // console.log(res.data);
          let code = res.data.statusCode;
          if (code === 200) {
            Toast.success('修改成功！');
            _this.returnFlag(true);
          }
          else{
            _this.returnFlag(false);
            Toast.fail('修改失败！');
          }
        });
      },
      returnFlag(flag) {
        let _this = this;
        if (flag) {
          _this.$emit("giveFlag", _this.randomWord(true, 10, 20));
        }
        else {
          _this.$emit("giveFlag", "null");
        }
      },
      getData(){
        let _this = this;
        let icon = _this.icon;
        if(icon === "男"){
          _this.sex = '0';
        }
        else if(icon === '女'){
          _this.sex = '1';
        }
      }
    },
    mounted() {
      let _this = this;
      setTimeout(function () {
        _this.getData();
      }, 300);
    }
  }
</script>

<style scoped>
</style>
